/**
 * Copyright (c) 2010-2011 Pierre Labatut
 *
 * This file is part of Space-Chronicles project.
 *
 * Space-Chronicles is free software: you can redistribute it and/or modify it
 * under the terms of its license which may be found in the accompanying
 * LICENSE file or <http://code.google.com/p/space-chronicles/>.
 */
package space.input;

import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import net.java.games.input.Component;
import net.java.games.input.Component.Identifier;
import net.java.games.input.Controller;
import net.java.games.input.Controller.Type;

/**
 *
 * @author Pierre
 */
public class CtlController {

    final Controller ctrl;
    final List<CtlPollListener> listeners = new CopyOnWriteArrayList<CtlPollListener>();

    public CtlController(final Controller ctrl) {
        if (ctrl == null) {
            throw new IllegalArgumentException();
        }
        this.ctrl = ctrl;
    }

    public void addPollListener(final CtlPollListener listener) {
        listeners.add(listener);
    }

    public void removePollListener(final CtlPollListener listener) {
        if (listeners.remove(listener) == false) {
            throw new IllegalStateException();
        }
    }

    public boolean poll() {
        final boolean res = ctrl.poll();
        if (res == true) {
            for (final CtlPollListener lstn : listeners) {
                lstn.onPoll(this);
            }
        }
        return res;
    }

  
    public String name() {
        return ctrl.getName();
    }

    public Component[] components() {
        return ctrl.getComponents();
    }

    public Component component(final Identifier id) {
        if (id == null) {
            throw new IllegalArgumentException();
        }
        return ctrl.getComponent(id);
    }
}
